package com.qding.park.common.core.aspect;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.qding.park.common.core.annotation.MethodCachEvict;
import com.qding.park.common.core.annotation.MethodCacheable;
import com.qding.park.common.core.aspect.handle.MethodCacheableHandler;
import com.qding.park.common.core.redis.JedisClient;

/**
 * @ClassName CacheAspect
 * @Description TODO(缓存切面)
 * @author Administrator
 * @Date 2016年9月5日 下午10:06:59
 * @version 1.0.0
 */
@Component
@Aspect
public class CacheAspect {

	private final static Log _log = LogFactory.getLog(CacheAspect.class);

	/**
	 * @Field @resultTag : TODO(业务结果变量)
	 */
	private final static String resultTag = "business_result";

	/**
	 * @Field @jedisClient : TODO(redisClient)
	 */
	@Autowired
	private JedisClient jedisClient;
	
	

	/**
	 * @Description (使用缓存切面)
	 * @param pjp
	 * @return
	 * @throws Throwable
	 */
	@Around("@annotation(com.qding.park.common.core.annotation.MethodCacheable)")
	public Object cacheAble(ProceedingJoinPoint pjp) throws Throwable {
		Method method = getMethod(pjp);
		MethodCacheable cacheable = method.getAnnotation(com.qding.park.common.core.annotation.MethodCacheable.class);
		MethodCacheableHandler handle=new MethodCacheableHandler();
		return handle.doCache(cacheable, pjp,jedisClient);
	}

//	@Around("@annotation(com.qding.park.common.core.annotation.MethodCachePut)")
	public Object cachePut() {

		return null;
	}

	/**
	  * cacheEvict(清除缓存注解 处理方式)
	  *
	  * @Title: cacheEvict
	  * @Description: TODO
	  * @param @return    设定文件
	  * @return Object    返回类型
	  * @throws
	  */
	@Around("@annotation(com.qding.park.common.core.annotation.MethodCachEvict)")
	public Object cacheEvict(ProceedingJoinPoint pjp) {
		Method method = getMethod(pjp);
		MethodCachEvict cachEvict = method.getAnnotation(com.qding.park.common.core.annotation.MethodCachEvict.class);
		
		return null;
	}

	@Around("@annotation(com.qding.park.common.core.annotation.MethodCaching)")
	public Object caching() {

		return null;

	}

//	private static boolean isJavaClass(Class<?> clz) {
//		return clz != null && clz.getClassLoader() == null;
//	}

	private Method getMethod(ProceedingJoinPoint pjp) {
		MethodSignature signature = (MethodSignature) pjp.getSignature();
		Method method = signature.getMethod();

		return method;

	}

//	public static void main(String[] args) {
//		ExpressionParser parser = new SpelExpressionParser();
//		Expression exp = parser.parseExpression(" 'Hello,World' ");
//		System.out.println((String) exp.getValue());
//	}
}
